package com.example.skyremote;





import androidx.appcompat.app.AppCompatActivity;

import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.view.View;
import java.io.IOException;
import android.util.Log;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity {
    private LocalSocket localSocket = null;
    private ReadThread mReadThread = null;
    private ByteBuffer mReadBuffer = ByteBuffer.allocate(4096);
    private boolean isConnect = false; // 假设这是你的类中的一个字段

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void gohead_click(View view) {

    }

    public void goleft_click(View view) {

    }

    public void goright_click(View view) {

    }

    public void goback_click(View view) {

    }

    public void remcnt(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {


                    localSocket = new LocalSocket(LocalSocket.SOCKET_DGRAM);
                    localSocket.bind(new LocalSocketAddress("uart3app"));
                    localSocket.connect(new LocalSocketAddress("uart3endpoint"));
                } catch (IOException e) {
                    e.printStackTrace();
                    // 处理异常（可能需要关闭socket或其他操作）
                    isConnect = false; // 根据需要调整连接状态
                }

            }
        }).start();

    }

    public void remset(View view) {
    }


    private class ReadThread extends Thread {
        @Override
        public void run() {
            try {
                int size = 0;
                while (isConnect && !isInterrupted()) {
                    InputStream inputStream = localSocket != null ? localSocket.getInputStream() : null;
                    if (inputStream == null) {
                        return;
                    }
                    if (inputStream.available() == 0) {
                        continue;
                    }
                    // 注意：这里直接使用mReadBuffer的array()方法可能不安全，
                    // 因为ByteBuffer的位置和限制可能没有正确设置。
                    // 我们应该使用ByteBuffer的正确方法来读取数据。
                    mReadBuffer.clear(); // 清除缓冲区，准备写入新数据
                    size = inputStream.read(mReadBuffer.array(), mReadBuffer.position(), mReadBuffer.remaining());
                    if (size > 0) {
                        byte[] tempArray = new byte[size];
                        mReadBuffer.get(tempArray, 0, size); // 从ByteBuffer复制到byte数组
                        // 假设String2ByteArrayUtils是你的工具类，下面这行代码需要相应调整
                        Log.e("ReadThread", "receive：" + String2ByteArrayUtils.bytes2Hex(tempArray));
                    }
                    mReadBuffer.clear(); // 再次清除缓冲区，为下一次读取做准备
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
